package com.mllweb.blog.web.controller;

import com.mllweb.blog.base.BaseController;
import com.mllweb.blog.core.digest.Base64Utils;
import com.mllweb.blog.data.orm.BlogArticle;
import com.mllweb.blog.data.orm.BlogUserCategory;
import com.mllweb.blog.service.ArticleService;
import com.mllweb.blog.service.CategoryService;
import com.mllweb.blog.web.dto.User;
import com.mllweb.blog.web.session.SessionKey;
import com.sun.org.apache.xml.internal.utils.CharKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sun.applet.Main;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.events.Characters;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.spec.EncodedKeySpec;
import java.util.List;

/**
 * 文章Controller
 */
@Controller
public class ArticleController extends BaseController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "article/{id}", method = RequestMethod.GET)
    private String article(@PathVariable String id, Model model) throws ServletException, IOException {
        boolean notFound = false;
        BlogArticle article = null;
        try {
            id = Base64Utils.decodeAsHex(id);
            article = articleService.select(Integer.parseInt(id));
            if (article == null) notFound = true;
        } catch (Exception e) {
            notFound = true;
        }
        if (notFound) {
            errorStatus(HttpStatus.NOT_FOUND);
        } else {
            model.addAttribute("article", article);
        }
        return "article/article";
    }

    @RequestMapping(value = "write", method = RequestMethod.GET)
    private String write(Model model) {
        List<BlogUserCategory> category = categoryService.selectByUserId();
        model.addAttribute("category", category);
        return "article/write";
    }

    @RequestMapping(value = "write/category/{categoryId}", method = RequestMethod.GET)
    private String category(@PathVariable int categoryId, Model model) {
        List<BlogUserCategory> category = categoryService.selectByUserId();
        model.addAttribute("category", category);
        model.addAttribute("categoryId", categoryId);
        List<BlogArticle> article = articleService.selectByCategoryId(categoryId);
        model.addAttribute("article", article);
        return "article/write";
    }

    @RequestMapping(value = "write/category", method = RequestMethod.POST)
    private String category(BlogUserCategory category) {
        categoryService.insert(category);
        return redirect("/write");
    }
    @RequestMapping(value = "write/article", method = RequestMethod.POST)
    private String article(int categoryId) {
        BlogArticle article=new BlogArticle();
        articleService.insert(article,categoryId);
        return redirect("/write");
    }

}
